package ui;

import bean.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class LoginUI extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton cancelButton;

    private static ArrayList<User> allUsers = new ArrayList<>();

    static {
        allUsers.add(new User("admin", "123456", "admin"));
        allUsers.add(new User("test", "123456", "test"));
        allUsers.add(new User("test1", "123456", "test1"));
        allUsers.add(new User("test2", "123456", "test2"));
        allUsers.add(new User("万朗", "111111", "wl"));

    }


    public LoginUI() {
        initializeComponents();
        setupLayout();
        setupEventHandlers();
        setupWindowProperties();
    }

    private void initializeComponents() {
        // 设置标题
        setTitle("员工管理系统 - 用户登录");

        // 初始化组件
        usernameField = new JTextField(15);
        passwordField = new JPasswordField(15);
        loginButton = new JButton("登录");
        cancelButton = new JButton("取消");

        // 设置组件属性
        usernameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 设置登录按钮为默认按钮
        getRootPane().setDefaultButton(loginButton);
    }

    private void setupLayout() {
        // 设置主面板布局
        setLayout(new BorderLayout());

        // 创建标题标签
        JLabel titleLabel = new JLabel("员工管理系统", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
        titleLabel.setForeground(new Color(0, 102, 204));
        add(titleLabel, BorderLayout.NORTH);

        // 创建中心面板用于放置表单
        JPanel centerPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        // 用户名标签和输入框
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        centerPanel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        centerPanel.add(usernameField, gbc);

        // 密码标签和输入框
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        centerPanel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        centerPanel.add(passwordField, gbc);

        add(centerPanel, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        buttonPanel.add(loginButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置边框
        centerPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "登录信息"));
    }

    private void setupEventHandlers() {
        // 登录按钮事件处理
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleLogin();
            }
        });

        // 取消按钮事件处理
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        // 密码框回车事件处理
        passwordField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleLogin();
            }
        });
    }

    private void handleLogin() {
        login();
    }

    private void login() {


        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        // 简单验证
        if (username.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入用户名！", "提示", JOptionPane.WARNING_MESSAGE);
            usernameField.requestFocus();
            return;
        }

        if (password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入密码！", "提示", JOptionPane.WARNING_MESSAGE);
            passwordField.requestFocus();
            return;
        }
        User user = getUserByLoginName(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                new EmployeeManagerUI(user.getUsername()).setVisible(true);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "用户名或密码错误，请重新输入！", "登录失败", JOptionPane.ERROR_MESSAGE);
                passwordField.setText("");
                usernameField.requestFocus();
            }

        }


    }
    private User getUserByLoginName (String loginName){
        for (int i = 0; i < allUsers.size(); i++) {
            User user = allUsers.get(i);
            if (user.getLoginName().equals(loginName)) {
                return user;
            }
        }
        return null;
    }
    private void setupWindowProperties () {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null); // 居中显示
        setResizable(true); // 可以调整窗口大小
    }
}